package indi.kider.server.applet.api;

import indi.kider.common.model.apartment.*;
import indi.kider.server.applet.model.User;
import indi.kider.server.applet.service.CommService;
import indi.kider.server.applet.service.NewLockService;
import indi.kider.server.applet.service.RentLockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Slf4j
@RequestMapping(value = "lock", produces = "application/json;charset=UTF-8")
public class LockController {

    @Autowired
    private NewLockService lockService;

    @Autowired
    private RentLockService rentLockService;
    @Autowired
    private CommService commService;

    @GetMapping("applet/getLockInfo/{houseId}")
    public CommonResponse<DevLock> getLockInfo(@PathVariable(value = "houseId") String houseId){
        try {
            return CommonResponse.buildGetSuccessResponse(lockService.getLockInfo(houseId));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    @DeleteMapping("applet/delLockInfo/{houseId}")
    public CommonResponse<String> delLockInfo(@PathVariable(value = "houseId") String houseId){
        try {
            return CommonResponse.buildDelSuccessResponse(lockService.delLockInfo(houseId));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    @PostMapping("applet/updateLockInfo")
    public CommonResponse<String> updateLockInfo(@RequestBody DevLock devLock){
        try {
            return CommonResponse.buildUpdateSuccessResponse(lockService.updateLockInfo(devLock));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    @PostMapping("/getRentLockInfo")
    public CommonResponse<RentLock> getRentLockInfo(@RequestBody RentLock rentLock, HttpServletRequest request){
        try {
            User user = commService.getCurrentUser(request,new User());
            CommonResponse<RentLock> s = CommonResponse.buildGetSuccessResponse(rentLockService.appletGetRentLock(rentLock,user));
            return s;
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 添加卡信息
     * @param devLockCard
     * @param request
     * @return
     */
    @PostMapping("/addCard")
    public CommonResponse<String> addCard(@RequestBody DevLockCard devLockCard, HttpServletRequest request){
        try {
            User user = commService.getCurrentUser(request,new User());
            CommonResponse<String> s = CommonResponse.buildAddSuccessResponse(lockService.addCard(devLockCard,user));
            return s;
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 删除卡信息
     * @param devLockCard
     * @param request
     * @return
     */
    @PostMapping("/delCard")
    public CommonResponse<String> delCard(@RequestBody DevLockCard devLockCard, HttpServletRequest request){
        try {
            User user = commService.getCurrentUser(request,new User());
            CommonResponse<String> s = CommonResponse.buildDelSuccessResponse(lockService.delCard(devLockCard,user));
            return s;
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 查询卡信息
     * @param devLockCard
     * @param request
     * @return
     */
    @PostMapping("/getCardInfo")
    public CommonResponse getCardInfo(@RequestBody DevLockCard devLockCard, HttpServletRequest request){
        try {
            User user = commService.getCurrentUser(request,new User());
            return lockService.getCardInfo(devLockCard,user);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 添加指纹信息
     * @param devLockFingerPrint
     * @param request
     * @return
     */
    @PostMapping("/addFingerprint")
    public CommonResponse<String> addFingerprint(@RequestBody DevLockFingerPrint devLockFingerPrint, HttpServletRequest request){
        try {
            User user = commService.getCurrentUser(request,new User());
            CommonResponse<String> s = CommonResponse.buildAddSuccessResponse(lockService.addFingerprint(devLockFingerPrint,user));
            return s;
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 删除指纹信息
     * @param devLockFingerPrint
     * @param request
     * @return
     */
    @PostMapping("/delFingerprint")
    public CommonResponse<String> delFingerprint(@RequestBody DevLockFingerPrint devLockFingerPrint, HttpServletRequest request){
        try {
            User user = commService.getCurrentUser(request,new User());
            CommonResponse<String> s = CommonResponse.buildDelSuccessResponse(lockService.delFingerprint(devLockFingerPrint,user));
            return s;
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 查询指纹信息
     * @param devLockFingerPrint
     * @param request
     * @return
     */
    @PostMapping("/queryFingerPrint")
    public CommonResponse queryFingerPrint(@RequestBody DevLockFingerPrint devLockFingerPrint, HttpServletRequest request){
        try {
            User user = commService.getCurrentUser(request,new User());
            return lockService.queryFingerPrint(devLockFingerPrint,user);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 添加锁具版本信息
     * @param devLockVersion
     * @param request
     * @return
     */
    @PostMapping("/addLockVersionInfo")
    public CommonResponse<String> addLockVersionInfo(@RequestBody DevLockVersion devLockVersion, HttpServletRequest request){
        try {
            User user = commService.getCurrentUser(request,new User());
            CommonResponse<String> s = CommonResponse.buildAddSuccessResponse(lockService.addLockVersionInfo(devLockVersion,user));
            return s;
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 查询锁具版本信息
     * @param devLockVersion
     * @param request
     * @return
     */
    @PostMapping("/queryLockVersionInfo")
    public CommonResponse<List<DevLockVersion>> queryLockVersionInfo(@RequestBody DevLockVersion devLockVersion, HttpServletRequest request){
        try {
            User user = commService.getCurrentUser(request,new User());
            CommonResponse<List<DevLockVersion>> s = CommonResponse.buildGetSuccessResponse(lockService.queryLockVersionInfo(devLockVersion,user));
            return s;
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 添加用户锁具密码信息
     * @param devLockPassword
     * @param request
     * @return
     */
    @PostMapping("/addLockPassword")
    public CommonResponse<String> addLockPassword(@RequestBody DevLockPassword devLockPassword, HttpServletRequest request){
        try {
            User user = commService.getCurrentUser(request,new User());
            CommonResponse<String> s = CommonResponse.buildAddSuccessResponse(lockService.addLockPassword(devLockPassword,user));
            return s;
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 删除用户锁具密码信息
     * @param devLockPassword
     * @param request
     * @return
     */
    @PostMapping("/delLockPassword")
    public CommonResponse<String> delLockPassword(@RequestBody DevLockPassword devLockPassword, HttpServletRequest request){
        try {
            User user = commService.getCurrentUser(request,new User());
            CommonResponse<String> s = CommonResponse.buildDelSuccessResponse(lockService.delLockPassword(devLockPassword,user));
            return s;
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 查询用户锁具密码信息
     * @param devLockPassword
     * @param request
     * @return
     */
    @PostMapping("/queryLockPassword")
    public CommonResponse queryLockPassword(@RequestBody DevLockPassword devLockPassword, HttpServletRequest request){
        try {
            User user = commService.getCurrentUser(request,new User());
            return lockService.queryLockPassword(devLockPassword,user);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }
}
